package com.yunupmall.controller.amdin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunupmall.api.fordVO.KindPo;
import com.yunupmall.api.fordVO.KindVO;
import com.yunupmall.api.fordentity.Commodity;
import com.yunupmall.api.fordentity.Kind;
import com.yunupmall.api.po.PagePO;
import com.yunupmall.service.admin.service.CommodityService;
import com.yunupmall.service.admin.service.KindService;
import com.yunupmall.uits.Result;
import com.yunupmall.uits.ReturnData;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/kind")
@AllArgsConstructor
public class KindController {
   private final KindService kindService;
   private final CommodityService commodityService;
   private final Result result;


    /**
     * 获取类别信息
     */

    @GetMapping("/byid")
    private Result byid(@RequestParam Integer id){
        try {
            return result.ok(kindService.byid(id));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
    /**
     * 新增商品规格
     * @return
     */
    @PostMapping(value = "/add")
    public Result<String> insertKind(@RequestBody Kind kind) {
        try {
            return result.ok(kindService.AddKind(kind));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    @PostMapping("/page")
    public Result<Page<Kind>> getAllkindData(@RequestParam(value = "curr") Integer count,@RequestParam(value = "nums") Integer size,@RequestParam(value = "name",required = false) String name) {
        try {
            PagePO pagePO = new PagePO();
            pagePO.setCurrent(count);
            pagePO.setSize(size);
            KindPo kindPo = new KindPo();
            kindPo.setPagePO(pagePO);
            if (ObjectUtil.isNotEmpty(name)){
                kindPo.setName(name);
            }
            return result.ok(kindService.pageKind(kindPo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
//
//    @RequestMapping("/deposit")
//    public void setData(@RequestParam("id") Integer id) {
//        cId = id;
//    }
//
//    @RequestMapping("/queryKindUpdata")
//    public String queryKindUpdata(@RequestParam("thispage") Integer thispage, @RequestParam("sizepage") Integer pagesize) {
//        int i = commodityimpl.quetyCounts(cId);
//        List<KindUpDate> kindUpDates = commodityimpl.queryKindUpdata(cId, (thispage - 1) * pagesize, pagesize);
//        String returnData = ReturnData.getReturnData(i, kindUpDates);
//        return returnData;
//    }
//
    @RequestMapping("/delete")
    public Result<String> DeleteKindData(@RequestParam("id") Integer id) {
        try {
            return result.ok(kindService.deleteKindData(id));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
//
    //查询第一级类别目录
    @GetMapping("/getList")
    public Result<List<Kind>> quetykindData() {
        try {
            return result.ok(kindService.getListone());
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
    /**
     * 通过当前类别获取商品信息
     */
    @PostMapping("/pageCommtity")
    public Result<Page<Commodity>> pageCommtity(@RequestBody KindVO kindVo){
        try {
            return result.ok(kindService.pageCommtity(kindVo));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
}
